Shader "Custom/InvColorWithAlpha"
{
    Properties
    {
        _MainTex("Image", 2D) = "white" {}
    }
        SubShader
    {
        Pass
        {
            Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }

            Blend SrcColor DstColor
            BlendOp Sub

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct a2v
            {
                float4 pos: POSITION;
                float2 uv: TEXCOORD0;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv: TEXCOORD0;
                float4 color : COLOR;
            };

            sampler2D _MainTex;
            uniform float _Alpha;

            v2f vert(a2v i)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(i.pos);
                o.uv = i.uv;
                o.color = i.color;
                return o;
            }

            fixed4 frag(v2f i) : COLOR
            {
                float4x4 thresholdMatrix =
                {
                    1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
                    13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
                    4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
                    16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
                };
                
                fixed4 color = tex2D(_MainTex, i.uv) * i.color;
                if (color.a < 0.01f) {
                    discard;
                }

                clip(i.color.a - thresholdMatrix[i.pos.x % 4][i.pos.y % 4]);

                return color;
            }

            ENDCG
        }
    }
    FallBack "UI/Default"
}
